Skip to content

Fix string handling in pushAny - #231

Merged
natecraddock merged 1 commit into
natecraddock:mainfrom
AshAmetrine:fix/pushany-strings
Oct 24, 2026
Merged

natecraddock merged 1 commit into
natecraddock:mainfrom
AshAmetrine:fix/pushany-strings

Conversation

@AshAmetrine

Copy link
Copy Markdown
Contributor

Changes:

  • Fixes support for null-terminated many-item pointers ([*:0]const u8)
  • Preserves embedded null bytes in strings
  • Avoids allocating temporary null-terminated copies
  • Updates the pushAny test to cover support for [*:0]const u8 and embedded null bytes in string literals

@robbielyman robbielyman left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of these changes are sort of surprising to me!

Comment thread src/lib.zig
Comment thread src/lib.zig
}
_ = lua.pushStringZ(std.mem.span(value));
},
.c => @compileError("C pointer length cannot be determined; pass a slice instead"),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this here? Lua itself obviously works with C pointers all the time?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quoting the Zig documentation: "The only valid reason for using a C pointer is in auto-generated code from translating C code."
I see no reason why they should be supported. The caller should know the type.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible that the value comes from another C library. I have recently been doing work using Lua and sqlite and I need to work with C strings occasionally. I think C pointers should be supported

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, you would either know the length or whether it has a null terminator, then you could use either ptr[0..len] or std.mem.span(ptr).

If we supported C pointers with pushAny, it would be making the assumption that it has a null terminator, which results in UB when there isn't one.

Side note: I did not remove support for C pointers in this change. I only changed the error message.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tested with this PR. This fails

const c_str: [*c]const u8 = "hello zig";
try lua.pushAny(c_str);
try testing.expectEqualStrings("hello zig", try lua.toString(-1));

and both of these work

const c_str: [*c]const u8 = "hello zig";
try lua.pushAny(std.mem.span(c_str));
try testing.expectEqualStrings("hello zig", try lua.toString(-1));

const c_str: [*c]const u8 = "hello zig";
const ptr: [*:0]const u8 = c_str;
try lua.pushAny(ptr);
try testing.expectEqualStrings("hello zig", try lua.toString(-1));

I gave it more thought and I think I'm actually okay with this change. If someone has a [*c] pointer, they can use span or coerce to a null-terminated many-item pointer.

Comment thread src/lib.zig
@AshAmetrine

Copy link
Copy Markdown
Contributor Author

@natecraddock Have you had a chance to look at my reply? Let me know if there are any changes you'd like me to make.

@natecraddock

Copy link
Copy Markdown
Owner

@AshAmetrine yes I took a look. I gave it more thought and I am okay with these changes. Tests are failing because this branch is outdated so I'll rebase on main and get it passing and merged.

@natecraddock
natecraddock merged commit 4aab3af into natecraddock:main Oct 24, 2026
3 checks passed
@natecraddock

Copy link
Copy Markdown
Owner

Merged! Sorry for taking a while to take another look

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants